<template>
  <div class="register-container">
    <div class="register-card">
      <el-icon class="github-logo">
        <Monitor />
      </el-icon>
      <h2>Sign up for VocabSphere</h2>

      <el-form :model="form" class="register-form">
        <el-form-item>
          <el-input v-model="form.username" placeholder="Username" size="large" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.email" placeholder="Email address" size="large" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.password" type="password" placeholder="Password" size="large" show-password />
        </el-form-item>

        <el-button type="primary" class="sign-up-btn" @click="handleRegister">Sign up</el-button>
      </el-form>

      <p class="terms">
        By signing up, you agree to our
        <a href="#">terms of service</a> and
        <a href="#">privacy policy</a>.
      </p>

      <div class="login-link">
        <p>Already have an account? <RouterLink to="/login">Sign in</RouterLink>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { register } from "../../api/authService.js";

const form = ref({
  username: "",
  email: "",
  password: "",
});

const loading = ref(false);
const router = useRouter();

const handleRegister = async () => {
  if (!form.value.username || !form.value.email || !form.value.password) {
    alert("请填写所有字段");
    return;
  }

  loading.value = true;

  const response = await register(form.value.username, form.value.email, form.value.password);

  if (response.success) {
    alert("注册成功！请登录");
    router.push("/login");
  } else {
    alert(response.message);
  }

  loading.value = false;
};
</script>

<style lang="scss" scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  color: var(--btn-text);
  background: linear-gradient(var(--bg-color-deg), var(--bg-color-change1), var(--bg-color-change2), var(--bg-color-change3));
  background-size: 400% 400%;
  animation: gradientAnimation 10s infinite alternate ease-in-out;
  transition: background 1s, color 1s;

  @keyframes gradientAnimation {
    0% {
      background-position: 0% 50%;
    }

    50% {
      background-position: 100% 50%;
    }

    100% {
      background-position: 0% 50%;
    }

  }
}

.register-card {
  background: var(--bg-color-flag);
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(255, 255, 255, 0.1);
  text-align: center;
  width: 350px;
  animation: fadeIn 0.6s ease-out forwards;

  @keyframes fadeIn {
    0% {
      opacity: 0;
      transform: translateY(-20px);
    }

    100% {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .github-logo {
    font-size: 3rem;
    // color: #fff;
  }

  h2 {
    // color: #fff;
    font-size: 1.5rem;
    margin-bottom: 1rem;
  }

  .register-form {
    .el-input {
      margin-bottom: 1rem;
    }
  }

  .sign-up-btn {
    width: 100%;
    background: #2f3e53;
    border: none;
    // color: white;
    font-weight: bold;
    transition: background 0.3s;

    &:hover {
      background: #272727;
    }
  }

  .terms {
    margin-top: 1rem;
    font-size: 0.8rem;
    color: #8b949e;

    a {
      color: #58a6ff;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  .login-link {
    margin-top: 1rem;
    font-size: 0.9rem;
    color: #8b949e;

    a {
      color: #58a6ff;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style>
